package com.hedl.practice.diyJava.regexs.study_group_matching.practice;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    //利用分组匹配，从字符串"23:01:59"提取时、分、秒。
    //static Pattern pattern = Pattern.compile("([0-23]\\w{0,2})\\:([0-60]\\w{0,2})\\:([0-60]\\w{0,2})");
    static Pattern pattern = Pattern.compile("^([01]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$");
    public static void main(String[] args) {
        System.out.println(pattern.matcher("23:01:59").matches());      //true
        Matcher matcher = pattern.matcher("23:01:59");
        if (matcher.matches()) {        //再次判断是否正确
            String a1 = matcher.group(0);       //输出完整时间
            String a2 = matcher.group(1);       //输出"时"
            String a3 = matcher.group(2);       //输出"分"
            String a4 = matcher.group(3);       //输出"秒"
            System.out.println(a1);
            System.out.println(a2);
            System.out.println(a3);
            System.out.println(a4);
        }
    }
}
